package com.tree.web.wechat;

import com.tree.entity.*;
import com.tree.service.*;
import com.tree.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/wechat")
public class WechatController {

    @Autowired
    private RtreeService rtreeService;
    @Autowired
    private FlowerService flowerService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private TreeNewService treeNewService;
    @Autowired
    private UserService userService;
    private static String str = "获取数据失败";
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    private Map<String,Object> getTreeList(HttpServletRequest request){

        Map<String,Object> modelMap = new HashMap<>();
        List<Rtree> rtreeList = new ArrayList<>();
        //获取一百条数据
        rtreeList = rtreeService.queryAll(0,3000);
        //判断是否获取到数据
        if (rtreeList.size() > 0) {
            modelMap.put("success",true);
            modelMap.put("treeList", rtreeList);
        }
        else{
            modelMap.put("success",false);
            modelMap.put("error",str);
        }
        return modelMap;
    }

    @RequestMapping(value = "/gettree",method = RequestMethod.GET)
    private Map<String,Object> getTree(HttpServletRequest request){

        Map<String,Object> modelMap = new HashMap<>();
        //获取树种名称
        String treeName = HttpUtil.getString(request,"treeName");
        Rtree rtree = rtreeService.query(treeName);
        if (rtree != null){
            modelMap.put("success",true);
            modelMap.put("tree",rtree);
        }else {
            modelMap.put("success",false);
            modelMap.put("error",str);
        }
        return modelMap;
    }

    @RequestMapping(value = "/flower",method = RequestMethod.GET)
    private Map<String,Object> getFlowerList(HttpServletRequest request){

        Map<String,Object> modelMap = new HashMap<>();
        //判断用户是否登录
        List<Flower> flowerList = new ArrayList<>();
        //获取3000条数据
        flowerList = flowerService.queryAll(0,3000);
        //判断是否获取到数据
        if (flowerList.size() > 0) {
            modelMap.put("success",true);
            modelMap.put("flowerList", flowerList);
        }
        else{
            modelMap.put("success",false);
            modelMap.put("error",str);
        }
        return modelMap;
    }

    @RequestMapping(value = "/getflower",method = RequestMethod.GET)
    private Map<String,Object> getFlower(HttpServletRequest request){

        Map<String,Object> modelMap = new HashMap<>();
        //获取树种名称
        String flowerName = HttpUtil.getString(request,"flowerName");
        Flower flower = flowerService.query(flowerName);
        if (flower != null){
            modelMap.put("success",true);
            modelMap.put("flower",flower);
        }else {
            modelMap.put("success",false);
            modelMap.put("error",str);
        }
        return modelMap;
    }

    @RequestMapping(value = "/article",method = RequestMethod.GET)
    private Map<String,Object> getArticleList(HttpServletRequest request){

        Map<String,Object> modelMap = new HashMap<>();
        List<Article> articleList = new ArrayList<>();
        //获取3000条数据
        articleList = articleService.queryAll(0,500);
        //判断是否获取到数据
        if (articleList.size() > 0) {
            modelMap.put("success",true);
            modelMap.put("articleList", articleList);
        }
        else{
            modelMap.put("success",false);
            modelMap.put("error",str);
        }
        return modelMap;
    }

    @RequestMapping(value = "/getarticle",method = RequestMethod.GET)
    private Map<String,Object> getArticle(HttpServletRequest request){

        Map<String,Object> modelMap = new HashMap<>();
        //获取树种名称
        String acName = HttpUtil.getString(request,"acName");
        Article article = articleService.query(acName);
        if (article != null){
            modelMap.put("success",true);
            modelMap.put("article",article);
        }else {
            modelMap.put("success",false);
            modelMap.put("error",str);
        }
        return modelMap;
    }
    @RequestMapping(value = "/new",method = RequestMethod.GET)
    private Map<String,Object> getNewrList(HttpServletRequest request){

        Map<String,Object> modelMap = new HashMap<>();
        List<TreeNew> newList = new ArrayList<>();
        //获取3000条数据
        newList = treeNewService.queryAll(0,500);
        //判断是否获取到数据
        if (newList.size() > 0) {
            modelMap.put("success",true);
            modelMap.put("newList", newList);
        }
        else{
            modelMap.put("success",false);
            modelMap.put("error",str);
        }
        return modelMap;
    }
    //注册
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    private Map<String,Object> register(@RequestBody User user){

        Map<String,Object> modelMap = new HashMap<>();
        //判断账号是否已经注册
        if (userService.query(user.getUserNum())){
            modelMap.put("success",false);
            modelMap.put("error","账号已注册,请直接登录");
            return modelMap;
        }
        //注册
        int num = userService.insert(user);
        if (num > 1){
            modelMap.put("success",true);
            modelMap.put("msg","注册成功");
        }
        return modelMap;
    }

    @RequestMapping(value = "/login")
    //登录
    private Map<String,Object> login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response){

        Map<String,Object> modelMap = new HashMap<>();
        //判断是否有此用户
        if (!userService.query(user.getUserNum())){
            modelMap.put("success",false);
            modelMap.put("error","没有此用户");
            return modelMap;
        }
        //判断密码是否正确
        if (!userService.queryAll(user)){
            modelMap.put("success",false);
            modelMap.put("error","密码错误");
            return modelMap;
        }
        modelMap.put("success",true);
        modelMap.put("user",user);
        modelMap.put("msg","登录成功");
        return modelMap;
    }
}
